package custom;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qianfeng.lingshijia.R;
import com.qianfeng.lingshijia.WebViewActivity;

import java.util.List;

import entity.HeadEntity;
import utils.DownloadUtils;

/**
 * Created by Mr.xingkong on 2016/1/11.
 */
public class HeadViewPager extends FrameLayout implements ViewPager.OnPageChangeListener {
    private Context context;
    private LoopViewPager viewPager;
    private NaviAnimView naviAnimView;


    private List<HeadEntity.DataEntity.BannerListEntity> data;

    public HeadViewPager(Context context)
    {
        super(context);
        this.context = context;
        init();
    }
    public HeadViewPager(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init()
    {
        LayoutInflater.from(context).inflate(R.layout.head_view_pager, this);
        viewPager = (LoopViewPager) findViewById(R.id.loopViewPager);
        naviAnimView = (NaviAnimView) findViewById(R.id.naviAnimView);
    }

    public void setData(List<HeadEntity.DataEntity.BannerListEntity> data)
    {
        this.data = data;
        initView();
    }

    private void initView()
    {
        if (data != null)
        {
            naviAnimView.setCount(data.size());
            MyPagerAdapter adapter = new MyPagerAdapter();
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(this);


        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        naviAnimView.setIndexAnim(position,positionOffset);
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class MyPagerAdapter extends PagerAdapter
    {

        @Override
        public int getCount()
        {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position)
        {
            ImageView imageView = new ImageView(context);
            imageView.setAdjustViewBounds(true);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,150);
            imageView.setLayoutParams(params);
            DownloadUtils.LoadPicture(data.get(position).getPicUrl(), imageView);
            container.addView(imageView);

            imageView.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    String url = data.get(position).getLinkUrl();
                    url = "http://m.haolingzui"+url.substring(19,url.length());
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url",url);
                    context.startActivity(intent);
                }
            });

            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            container.removeView((View) object);
        }
    }

}
